/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.departamentos;

import com.mapping.Departamento;
import com.opensymphony.xwork2.ActionSupport;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Andres
 */
@Namespace(value="/")
@Action(value="buscarDepartamentos", results={@Result(name="success",location="/GestionDepartamentos/gestionDepartamentos.jsp")})
public class BuscarDepartamentosAction extends ActionSupport{
    
    private List<Departamento> listaDepartamento;
    private String buscar;
    
    @Override
    public String execute(){
        String consulta = "select IDDEPARTAMENTO,NOMBRE,DESCRIPCION,LOCALIZACION from departamento where match (idDepartamento,nombre,descripcion,localizacion) against (?1 IN BOOLEAN MODE)"; 
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if(buscar== null || buscar.equals("")){
            listaDepartamento = (List<Departamento>) em.createNamedQuery("Departamento.findAll").getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta,Departamento.class);
            query.setParameter(1,buscar);
            listaDepartamento = (List<Departamento>) query.getResultList();
        }
        tx.commit();
        em.close();
         
         return SUCCESS;
    }

    public List<Departamento> getListaDepartamento() {
        return listaDepartamento;
    }

    public String getBuscar() {
        return buscar;
    }


    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }

    
}
